//
//  Player.m
//  嗨饭喽
//
//  Created by dllo on 15/5/29.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#define Width [UIScreen mainScreen].bounds.size.width
#define Height [UIScreen mainScreen].bounds.size.height
#import "Player.h"

@interface Player ()

@property (nonatomic, assign) float w;
@property (nonatomic, assign) float h;

@end

@implementation Player

#pragma mark -
#pragma mark Compiler Directives & Static Variables
@synthesize movieURL;

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.w = Width / 375.0;
    self.h = Height / 667.0;
    // 加载动画
    loadingAni = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(165 / 375.0 * Width, 300 / 667.0 * Height, 37 / 375.0 * Width, 37 / 667.0 * Height)];
    loadingAni.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
    [self.view addSubview:loadingAni];
    
    // 加载提醒
    label = [[UILabel alloc] initWithFrame:CGRectMake(150 * self.w, 340 * self.h, 80 * self.w, 40 * self.h)];
    label.text = @"加载中...";
    label.textColor = [UIColor whiteColor];
    label.backgroundColor = [UIColor clearColor];
    
    [[self view] setBackgroundColor:[UIColor blackColor]];
    [loadingAni startAnimating];
    [self.view addSubview:label];
    
    UIButton *back = [[UIButton alloc] initWithFrame:CGRectMake(150 / 375.0 * Width, 380 / 667.0 * Height, 67 / 375.0 * Width, 37 / 667.0 * Height)] ;
    [back setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [back setTitle:@"cancel" forState:UIControlStateNormal];
    [back addTarget:self action:@selector(moviePlayBackDidFinish:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:back];
}

- (void)moviePlayerLoadStateChanged:(NSNotification*)notification
{
    [loadingAni stopAnimating];
    [label removeFromSuperview];
    
    // Unless state is unknown, start playback
    if ([mp loadState] != MPMovieLoadStateUnknown)
    {
        // Remove observer
        [[NSNotificationCenter defaultCenter] removeObserver:self
                                                            name:MPMoviePlayerLoadStateDidChangeNotification
                                                          object:nil];
        
        //设置横屏
        [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];
        // Rotate the view for landscape playback
        [[self view] setBounds:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
        [[mp view] setFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
        
        // Add movie player as subview
        [[self view] addSubview:[mp view]];
        
        // Play the movie
        [mp play];
    }
}


- (void)moviePlayBackDidFinish:(NSNotification*)notification
{
    //[[UIApplication sharedApplication] setStatusBarHidden:YES];
    //还原状态栏为默认状态
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:NO];
    // Remove observer
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                        name:MPMoviePlayerPlaybackDidFinishNotification
                                                      object:nil];
    
    [self dismissModalViewControllerAnimated:NO];
}


- (void)readyPlayer
{
    mp =  [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
    
    if ([mp respondsToSelector:@selector(loadState)])
    {
        // Set movie player layout
        [mp setControlStyle:MPMovieControlStyleFullscreen];        //MPMovieControlStyleFullscreen        //MPMovieControlStyleEmbedded
        //满屏
        [mp setFullscreen:YES];
        // 有助于减少延迟
        [mp prepareToPlay];
        
        // Register that the load state changed (movie is ready)
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(moviePlayerLoadStateChanged:)
                                                     name:MPMoviePlayerLoadStateDidChangeNotification
                                                   object:nil];
    }
    else
    {
        // Play the movie For 3.1.x devices
        [mp play];
    }
    
    // Register to receive a notification when the movie has finished playing.
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(moviePlayBackDidFinish:)
                                                 name:MPMoviePlayerPlaybackDidFinishNotification
                                               object:nil];
}


@end
